package com.tencent.mttpacketcapture;

import java.lang.reflect.Method;
import java.util.List;

import jp.co.taosoftware.android.packetcapture.PacketCaptureService;
import android.app.Activity;
import android.content.ActivityNotFoundException;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.net.VpnService;
import android.os.Build;
import android.os.Bundle;
import android.os.Parcelable;
import android.widget.Toast;

public class Notify extends Activity{
	public static final String mActionUpload = "com.tencent.mttpacketcapture.uploadpacket";
	public static final String mActionStart = "com.tencent.mttpacketcapture.startCaputre";
	public static final String mActionFinish = "com.tencent.mttpacketcapture.finishCapture";
	
	private Intent mItent = null;
	public static Parcelable sVpnConfig = null;
	
	private Intent getServiceIntent(){
		if( mItent == null ){
			Intent remote = getIntent();
        	String sQUA = remote.getStringExtra("qua");
            String sQGUID = remote.getStringExtra("qguid");
            
			mItent = new Intent(this, PacketCaptureService.class);
			mItent.putExtra("PcapPath", Utility.genPacketPath());
			mItent.putExtra("qua", sQUA);
			mItent.putExtra("qguid", sQGUID);
		}
		return mItent;
	}
	
	private void sdkR14StartCapture(Intent intent)
	{
		if (android.os.Build.VERSION.RELEASE.equalsIgnoreCase("4.4.3") || android.os.Build.VERSION.RELEASE.equalsIgnoreCase("4.4.4"))
		{
			Toast.makeText(Notify.this, "Android 4.4.3抓包工具可能工作不正常。", Toast.LENGTH_LONG).show();
		}
		
		Intent vpnintent = VpnService.prepare(this);
		if (vpnintent != null) {
			try{
				startActivityForResult(vpnintent, 0);
			}
			catch (ActivityNotFoundException ex)
			{
				ex.printStackTrace();
			}
		} else {
			onActivityResult(0, RESULT_OK, intent);
		}
	}
	
	@Override
	protected void onNewIntent(Intent intent) {
		setIntent(intent);
		super.onNewIntent(intent);
	}
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		collapseStatusBar(Notify.this);
				
		final String action = getIntent().getAction();
		
		if ( action.equalsIgnoreCase(mActionStart) ){
			sdkR14StartCapture(getIntent());
		}
		else if(action.equalsIgnoreCase(mActionFinish))
		{
			Intent mItent = new Intent(this, PacketCaptureService.class);
			mItent.putExtra("qguid", "rocksnzhang");
			startService(mItent);
			finish();
		}
		
		
	}
	
	@Override
    protected void onActivityResult(int request, int result, Intent data) {
        if (result == RESULT_OK) {
            startService(getServiceIntent());
        }
		  
		// 通知调用方（QB）开始抓包了
        try {
			Intent in = new Intent();
			in.setAction("com.tencent.QQBrowser.action.NETPACKETCAPTURE");
			in.putExtra("result", "NOTIFY_CLIENT_CAPTURE_START");
			
			final PackageManager packageManager = getPackageManager();
		    List<ResolveInfo> resolveInfo =  
		            packageManager.queryIntentActivities(in,  
		                    PackageManager.MATCH_DEFAULT_ONLY);
		    
		    //检查是否存在此Action，其实就是判断是否是QB-Blink  启动QB
		    if (resolveInfo.size() > 0) {
				startActivity(in);
		    }
        }
        catch ( Exception e ) {
        	e.printStackTrace();
        }
        
        finish();
	}
	
	private void collapseStatusBar( Context ctx)
	{
		try{
			Method close = null;
			Object sbar = ctx.getSystemService("statusbar");
			if (sbar == null ) return;
			
			if( Build.VERSION.SDK_INT <= 16){
				close = sbar.getClass().getMethod("collapse");
			}
			else
			{
				close = sbar.getClass().getMethod("collapsePanels");
			}
			if( close != null ){
				close.invoke(sbar);
			}
		}
		catch(Exception ex)
		{
			
		}
	}
}